package com.service.impl;


import com.dao.RbacManagerDao;
import com.entity.RbacManager;
import com.entity.ResponseEntity;
import com.service.RbacManagerService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RbacManagerServiceImpl implements RbacManagerService {
    @Autowired(required = false)
    RbacManagerDao rbacManagerDao;

    @Autowired
    RedisTemplate redisTemplate;


    @Override
    public ResponseEntity addOne(RbacManager rbacManager) {
        int i = rbacManagerDao.addOne(rbacManager);
        if(i > 0){
            return ResponseEntity.SUCCESSFUL;
        }else {
            return ResponseEntity.FAIL;
        }
    }

    @Override
    public ResponseEntity deleteOne(int id) {
        int i = rbacManagerDao.deleteOne(id);
        if(i > 0){
            return ResponseEntity.SUCCESSFUL;
        }else {
            return ResponseEntity.FAIL;
        }
    }

    @Override
    public ResponseEntity updateOne(RbacManager rbacManager) {
        int i =  rbacManagerDao.updateOne(rbacManager);
        System.out.println("i = " + i);
        if(i > 0){
            return ResponseEntity.SUCCESSFUL;
        }else {
            return ResponseEntity.FAIL;
        }
    }


    @Override
    public ResponseEntity find(int startPage,int pageSize,RbacManager rbacManager) {
        PageHelper.startPage(startPage, pageSize);
        List<RbacManager> all = rbacManagerDao.find(rbacManager);
        return new ResponseEntity(200,"成功",all);
    }

    @Override
    public RbacManager findByAccount(String account) {
        RbacManager manager = new RbacManager();
        manager.setAccount(account);
        List<RbacManager> rbacManagers = rbacManagerDao.find(manager);
        if(rbacManagers.size() == 1) {
            return rbacManagers.get(0);
        }
        return null;
    }

    @Override
    public RbacManager findByAccountAndPassword(String accout, String password) {
        RbacManager manager = new RbacManager();
        manager.setAccount(accout);
        manager.setPassword(password);
        List<RbacManager> rbacManagers = rbacManagerDao.find(manager);
        if(rbacManagers.size() == 1) {
            RbacManager rbacManager = rbacManagers.get(0);
            //使用redis缓存用户对象
            redisTemplate.opsForHash().put("rbacManager",accout,rbacManager);
            Object obj = redisTemplate.opsForHash().get("rbacManager",accout);
            System.out.println("obj = " + obj);
            System.out.println(" ===================================== ");
//            redisTemplate.boundHashOps(accout).put("rbacManager",rbacManager);
//            System.out.println(redisTemplate.boundHashOps(accout).get("rbacManager"));
            return rbacManager;
        }
        return null;
    }
}
